﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2009 , ESSE , Ltd.
//--------------------------------------------------------------------

using System.Runtime.Remoting;
using System.ServiceProcess;

namespace DotNet.WindowsService
{
    using DotNet.Utilities;

    /// <summary>
    /// CommonService
    ///
    /// 修改纪录
    ///
    ///		2008.03.06 版本：1.0 DotNet 创建Windows服务。
    ///
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2007.08.15</date>
    /// </author>
    /// </summary>
    public partial class CommonService : ServiceBase
    {
        public CommonService()
        {
            InitializeComponent();
        }

        public string GetAssemblyPath()
        {
            string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

            _CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 8);    // 8是 file:// 的长度

            string[] arrSection = _CodeBase.Split(new char[] { '/' });

            string _FolderPath = "";
            for (int i = 0; i < arrSection.Length - 1; i++)
            {
                _FolderPath += arrSection[i] + "\\";
            }
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(_FolderPath);
            if (dirInfo.Exists)
            {
                return dirInfo.FullName;
            }
            return _FolderPath;
        }

        protected override void OnStart(string[] args)
        {
            // 是否记录日志
            BaseSystemInfo.RecordLog = true;
            BaseSystemInfo.StartupPath = GetAssemblyPath();

            // 读取配置文件
            // 读取配置文件
            BaseSystemInfo.ConfigurationFrom = ConfigurationCategory.UserConfig;
            BaseConfiguration.GetSetting();

            // 设置数据库中的表配置
            // 主应用程序集名
            BaseSystemInfo.MainAssembly = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
            RemotingConfiguration.Configure(BaseSystemInfo.StartupPath + "\\" + ConfigHelper.FileName, false);
        }

        protected override void OnStop()
        {
        }
    }
}